using System;
using System.Data;
using System.IO;
using System.Text;
/*
 * This file is part of 
CSV Converter (CodeProject) is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
 
CSV Converter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser Public License for more details.
 
You should have received a copy of the GNU Lesser Public License
along with CSV Converter.  If not, see <http://www.gnu.org/licenses/>.
 * The Code Project Open License (CPOL)
 */

namespace TIS
{
	/// <summary>
	/// Summary description for CSVConvertor.
	/// </summary>
	public class CSVConvertor
	{
		/// <summary>
		/// To generate CSV file.
		/// </summary>
		/// <param name="oDataTable"></param>
		/// <param name="directoryPath"></param>
		/// <param name="fileName"></param>
		/// <returns></returns>
		//public string Convert(DataTable oDataTable, string directoryPath, string fileName)	
        public string Convert(DataTable oDataTable, string fullpath)	
		{

			//string fullpath = "";

            //if (directoryPath.Substring(directoryPath.Length - 1,1) == @"\" ||directoryPath.Substring(directoryPath.Length - 1,1) == "/")	
            //{
            //    fullpath = directoryPath + fileName;
            //}
            //else	
            //{
            //    fullpath = directoryPath + @"\" + fileName;
            //}

			StreamWriter SW;
			SW=File.CreateText(fullpath);
			
			
			StringBuilder oStringBuilder = new StringBuilder();

			
			/*******************************************************************
			 * Start, Creating column header
			 * *****************************************************************/

			foreach(DataColumn oDataColumn in oDataTable.Columns)	
			{
				oStringBuilder.Append(oDataColumn.ColumnName + ",");
			}

			SW.WriteLine(oStringBuilder.ToString().Substring(0,oStringBuilder.ToString().Length - 1));
			oStringBuilder.Length = 0;

			/*******************************************************************
			 * End, Creating column header
			 * *****************************************************************/

			/*******************************************************************
			 * Start, Creating rows
			 * *****************************************************************/

			foreach(DataRow oDataRow in oDataTable.Rows)	
			{
				
				
				foreach(DataColumn oDataColumn in oDataTable.Columns)	
				{
					oStringBuilder.Append(oDataRow[oDataColumn.ColumnName] + ",");
				}
				SW.WriteLine(oStringBuilder.ToString().Substring(0,oStringBuilder.ToString().Length - 1));
				oStringBuilder.Length = 0;

			}
			

			/*******************************************************************
			 * End, Creating rows
			 * *****************************************************************/


			SW.Close();

			return fullpath;

		}
	}
}
